package org.one.afternoon.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.one.afternoon.common.Result;
import org.one.afternoon.domain.ClientUser;
import org.one.afternoon.service.ClientUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/client")
public class ClientUserController {

    @Autowired
    private ClientUserService clientUserService;

    @CrossOrigin // 跨域访问
    @GetMapping("/all")
    public Result<List<ClientUser>> findAll() {
        List<ClientUser> clientUserList = clientUserService.list(null);
        return Result.success(clientUserList);
    }


    @CrossOrigin
    @PostMapping("/queryOne")
    @ResponseBody
    public Result<List<ClientUser>> queryOne(@RequestBody ClientUser clientUser) {
        LambdaQueryWrapper<ClientUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.likeRight(StringUtils.isNotBlank(clientUser.getPhone()), ClientUser::getPhone, clientUser.getPhone())
                .like(StringUtils.isNotBlank(clientUser.getUsername()), ClientUser::getUsername, clientUser.getUsername());
        List<ClientUser> list = clientUserService.list(lambdaQueryWrapper);
        return Result.success(list);
    }

    @CrossOrigin
    @PostMapping("/insertOne")
    @ResponseBody
    public Result<String> addOne(@RequestBody ClientUser clientUser) {
        boolean save = clientUserService.save(clientUser);
        if (!save) {
            return Result.error("添加用户失败");
        } else {
            return Result.success("添加用户成功");
        }
    }

    @CrossOrigin
    @PostMapping("/deleteOne")
    @ResponseBody
    public Result<String> deleteOne(@RequestParam Integer index) {
        clientUserService.removeById(index);
        Result<String> result = Result.success("删除成功");
        return result;
    }

    @CrossOrigin
    @PostMapping("/updateOne")
    @ResponseBody
    public Result<String> updateOne(@RequestBody ClientUser clientUser) {
        LambdaUpdateWrapper<ClientUser> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(StringUtils.isNotBlank(clientUser.getPhone()), ClientUser::getPhone, clientUser.getPhone())
                .set(StringUtils.isNotBlank(clientUser.getPhone()), ClientUser::getPhone, clientUser.getPhone())
                .set(StringUtils.isNotBlank(clientUser.getAvatar()), ClientUser::getAvatar, clientUser.getAvatar())
                .set(StringUtils.isNotBlank(clientUser.getPassword()), ClientUser::getPassword, clientUser.getPassword())
                .set(StringUtils.isNotBlank(clientUser.getUsername()), ClientUser::getUsername, clientUser.getUsername());
        clientUserService.update(null, lambdaUpdateWrapper);
        return Result.success("修改成功");
    }

    @CrossOrigin
    @PostMapping("/login")
    @ResponseBody
    public Result<String> login(@RequestBody ClientUser clientUser) {
        boolean login = clientUserService.login(clientUser);
        if (login) {
            return Result.success("登录成功");
        } else {
            return Result.error("登录失败");
        }
    }

    @CrossOrigin
    @PostMapping("/logout")
    @ResponseBody
    public Result<String> logout() {
        return Result.success("登出成功");
    }

    @CrossOrigin
    @PostMapping("/queryClient")
    @ResponseBody
    public Result<ClientUser> queryClient(@RequestBody ClientUser clientUser) {
        LambdaQueryWrapper<ClientUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(clientUser.getPhone()), ClientUser::getPhone, clientUser.getPhone());
        ClientUser one = clientUserService.getOne(lambdaQueryWrapper);
        return Result.success(one);
    }
}
